import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

import vuexPersist from "vuex-persist"

export default new Vuex.Store({
  state: {
    user: "", //保存用户名
    shoplist: [],
  },
  mutations: {
    setuser(state, val) {
      state.user = val;
    },

    exit(state) {
      state.user = "";
    },

    addshop(state, i) {
      let flag = false;
      state.shoplist.forEach((item) => {
        if (item.id == i.id) {
          flag = true;
          item.num++;
        }
      });
      if (flag == false) {
        Vue.set(i, "num", 1);

        state.shoplist.push(i)
      }
    },

    del(state, id) {
      state.shoplist.forEach((item, index) => {
        if (item.id == id) {
          state.shoplist.splice(index, 1);
        }
      });

    },
    plus(state, id) {
      state.shoplist.forEach((item) => {
        if (item.id = id) {
          item.num++;
        }
      })
    },
    minus(state, id) {
      state.shoplist.forEach((item) => {
        if (item.id = id) {
          item.num--;
        }
      })
    }

  },
  actions: {},
  modules: {},
  getters: {},
  plugins: [
    new vuexPersist({
      storage: window.localStorage,
    }).plugin,
  ],
})